package com.haitao.wavebbs.controller;

import com.haitao.wavebbs.entity.UserBasicInformation;
import com.haitao.wavebbs.entity.tools.CredentialForNomally;
import com.haitao.wavebbs.entity.User;
import com.haitao.wavebbs.service.UserService;
import com.haitao.wavebbs.tools.ControllerTools;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@RestController
public class UserController {

    public final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }
    /**
     * 用户注册接口
     * @param user：(必选项) userAccount、password
     * @return 若用户注册成功，则返回“创建成功”字段
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/user")
    public ResponseEntity userRegister(@RequestBody  User user) throws Exception {
        userService.userRegister(user);
        return ControllerTools.getSuccessResponseEntity(HttpStatus.OK,"创建成功");
    }

    /**
     * 用户登录接口；若验证成功则返回token字符串
     * @param credentialForNomally
     * @return token字符串  {userId,NickName,getUserAccount}
     */
    @CrossOrigin(origins = "*")
    @PostMapping("/accessToken")
    public ResponseEntity accessToken(@RequestBody CredentialForNomally credentialForNomally) throws Exception {
        String jwt = userService.accessToken(credentialForNomally);
        String authorization ="Bearer "+jwt;
//        return new ResponseEntity(new ResponseData("success",authorization), HttpStatus.CREATED);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("authorization",authorization);
        return ControllerTools.getSuccessResponseEntity(authorization, HttpStatus.CREATED,httpHeaders);
    }

    /**
     * 修改用户基本信息接口,该接口无需二次验证用户密码。该接口无法修改用户密码
     * @param user 用户基本信息，例如用户昵称、头像
     */
    @PatchMapping("/User")
    public ResponseEntity changeUserBasicInformation(@RequestBody User user){
        return null;
    }

    /**
     * 修改用户密码。该接口需二次验证用户密码
     * @param credentialForNomally   用户验证密码，用户新密码
     */
    @PutMapping("/User")
    public ResponseEntity changeUserPassword(@RequestBody CredentialForNomally credentialForNomally){
        return null;
    }

    /**
     * 从token中获取当前用户的userAccount，通过userId查询获得当前用户的基本信息
     * @return 当前用户的基本信息
     */
    @GetMapping("/User")
    public ResponseEntity getUserBasicInformation() throws Exception {
        int userId=0;
        UserBasicInformation userBasicInformation = userService.getUserBasicInformation(userId);
        return ControllerTools.getSuccessResponseEntity(userBasicInformation, HttpStatus.CREATED);
    }


}
